package com.yiguantong.police.ui.utils;

import android.app.Activity;
import android.content.Context;

import java.util.Stack;

/**
 * Activity 堆栈管理器
 * add by qiuxiaolong 2017/05/05
 */
public class ActivityStackManager {

    private static Stack<Activity> activityStack;
    private static ActivityStackManager instance;

    /**
     * 私有的构造方法
     */
    private ActivityStackManager() {
    }

    /**
     * 当前缓存Activity 实例个数
     */
    public static int getStackSize() {
        return activityStack == null ? 0 : activityStack.size();
    }

    /**
     * 单例的获取当前类的实例
     */
    public static ActivityStackManager getAppManager() {
        if (instance == null) {
            instance = new ActivityStackManager();
        }
        return instance;
    }

    /**
     * 将当前activity添加到activity管理栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 清空activity栈
     */
    public void finishAllActivity() {
        if (activityStack == null) {
            return;
        }
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 退出应用
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } catch (Exception e) {
        }
    }

}